import React, { Fragment } from 'react';
import { connect } from 'umi';
import { get as getPath } from 'object-path';
import { Row, Divider, Empty, Spin, message } from 'antd';
import BraftEditor from 'braft-editor';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import { withRoutePage } from '@/utils/enhanceUtils';

import Attachment from './components/Attachment';

import style from './NoticeDetail.less';

class NoticeDetail extends React.Component {
    componentDidMount() {
        this.fetchDetail();
        document.body.onselectstart = () => false;
        document.body.oncopy = () => false;

        // 禁止剪切
        document.body.oncut = () => false;
        // 禁止粘贴
        document.body.onpaste = () => false;
    }

    componentWillUnmount() {
        document.body.onselectstart = e => e;
        document.body.oncopy = e => e;

        // 禁止剪切
        document.body.oncut = e => e;
        // 禁止粘贴
        document.body.onpaste = e => e;
        const { dispatch } = this.props;
        dispatch({
            type: 'noticeManage/save',
            payload: {
                changeData: {
                    id: null,
                    content: null,
                    recvOrganizationCode: '',
                    startTime: null,
                    title: ''
                }
            }
        });
    }

    fetchDetail = () => {
        const { dispatch } = this.props;
        dispatch({
            type: 'noticeManage/fetchDetail',
            payload: {
                noticeId: getPath(this.props, 'location.query.noticeId')
            },
            callback: res => {
                if (res.code === 90003) {
                    message.error(res.msg);
                }
            }
        });
    };

    render() {
        const { loading, noticeManage } = this.props;
        const { changeData } = noticeManage;
        return (
            <Fragment>
                <PageHeaderWrapper>
                    <Spin spinning={loading}>
                        {changeData && changeData.id ? (
                            <div className={style.wncontainer}>
                                <Row className={style.title}>
                                    <p className={style.noticetitle}>{changeData.title}</p>
                                    <div className={style.noticeFrom}>
                                        <span className={style.spanMr}>{changeData.startTime}</span>
                                        <Divider type="vertical" />
                                        <span className={style.spanMr}>{changeData.recvOrganizationName}</span>
                                        <span className={style.spanMr}>{changeData.userName}</span>
                                    </div>
                                </Row>
                                {/* <Row
                                    className={
                                        (style.noticeText,
                                        ' braft-output-content')
                                    }
                                    dangerouslySetInnerHTML={{
                                        __html: changeData.content
                                    }}
                                /> */}
                                <Row className={style.noticeText}>
                                    <BraftEditor
                                        controls={[]}
                                        readOnly
                                        style={{
                                            border: 'none'
                                        }}
                                        value={BraftEditor.createEditorState(changeData.content)}
                                    />

                                    <div style={{ marginTop: 16 }}>
                                        {changeData?.noticeFile && (
                                            <Attachment attachmentList={changeData.noticeFile} type="download" />
                                        )}
                                    </div>
                                </Row>
                            </div>
                        ) : (
                            <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
                        )}
                    </Spin>
                </PageHeaderWrapper>
            </Fragment>
        );
    }
}
// export default NoticeDetail;
export default withRoutePage(
    connect(({ noticeManage, loading }) => ({
        noticeManage,
        loading: loading.models.noticeManage
    }))(NoticeDetail)
);
